import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_permission/yt_tab_view_model.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/pages/alarm/alarm_home/alarm_home_view.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_view.dart';
import 'package:yt_devops/pages/home_site_statistics/home_site_statistics_view.dart';
import 'package:yt_devops/pages/mine/mine/mine_view.dart';
import 'package:yt_devops/pages/operation_matainance/operation_matainance_view.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/site/site_view.dart';
import 'package:yt_devops/pages/site/widgets/home_statistics.dart';
import 'package:yt_devops/pages/tasks/task_list/task_list_view.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/utils/alarm_util.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';

class ProductManageHomeController extends GetxController
    with GetTickerProviderStateMixin {
  int selectedIndex = 0;
  // TabController? tabController;
  List<Widget> homeTabviewChildren = [];

  ///运维tabview
  bool firstIsStatic = false;

  ///首页第一个tab是否为统计
  bool firstIsTask = false;

  ///运维第一个tab是否为任务
  List<Widget> sitePages = [];
  List<BottomNavigationBarItem> items = [];
  List<YtTabViewModel> permissionGroups = [];
  @override
  void onInit() {
    super.onInit();

    if (FuncUtils.isPassAuthority([PermissionConstant.homeSiteList])) {
      homeTabviewChildren.add(
        PermissionWidget(
            permissionStringList: const [PermissionConstant.homeSiteList],
            child: KeepAliveWrapper(
              SiteHomePage(),
            )),
      );
    }
    if (FuncUtils.isPassAuthority([PermissionConstant.homeStatic])) {
      if (homeTabviewChildren.isEmpty) {
        firstIsStatic = true;

        ///如果homeTabviewChildren为空则说明第一个页面是统计
      }
      homeTabviewChildren.add(const PermissionWidget(
          permissionStringList: [PermissionConstant.homeStatic],
          child: KeepAliveWrapper(HomeStatistics())));
    }

    YtTabViewModel model1 = YtTabViewModel(
        permissionString: PermissionConstant.homeSite,
        labelString: ResKey.home,
        imageString: ImageAssets.siteSelected,
        permissionWidget: HomeSiteStatisticsPage(homeTabviewChildren,
            firstIsStatic: firstIsStatic));
    YtTabViewModel model2 = YtTabViewModel(
        key: 'operation',
        permissionString: PermissionConstant.operation,
        labelString: ResKey.operation,
        imageString: ImageAssets.omSelected,
        permissionWidget: const OperationMatainancePage());
    YtTabViewModel model3 = YtTabViewModel(
        permissionString: PermissionConstant.device,
        labelString: ResKey.device,
        imageString: ImageAssets.deviceSelected,
        permissionWidget: DeviceListPage());
    YtTabViewModel model4 = YtTabViewModel(
        permissionString: PermissionConstant.mine,
        labelString: ResKey.mine,
        imageString: ImageAssets.meSelected,
        permissionWidget: MinePage());
    permissionGroups.add(model1);
    permissionGroups.add(model2);
    permissionGroups.add(model3);
    permissionGroups.add(model4);
    // tabController = TabController(length: permissionGroups.length, vsync: this);
  }

  @override
  void onReady() {
    if (FuncUtils.isPassAuthority([
      PermissionConstant.homeSite,
      PermissionConstant.operation,
      PermissionConstant.device,
      PermissionConstant.mine
    ], mode: AuthorityModeType.exclude)) {
      Get.offAllNamed(RouterPath.noPermission, arguments: {'backLogin': true});
    }
  }

  @override
  void onClose() {}

  onItemTapped(index, YtTabViewModel? data) {
    selectedIndex = index;
    // tabController?.animateTo(index);
    if (data?.key == 'operation') {
      AlarmUtil.fromModule = AlarmFromWhereType.allSites.value;
    }
    update();
  }
}
